<?php
import("ORG.Util.Function");
import("@.Org.Utils");
import("@.Org.CommonTool");
import("@.Org.BaseTool");
import("@.Org.GoodstypeTool");

class GoodstypeAction extends CommonAction{	
	
	public function _initialize(){
		parent::_initialize();				
		$param           =    $this->_get('_URL_');
		$controller      =    $param[0];
		$action          =    $param[1];		
		//language
		$lang            =    L($action);  		
		$manage          =    L('manage');
		
		$this->assign("manage",$manage);
		$this->assign("current",$lang);
		$this->assign("lang",$lang);
	}
	
	public function _empty(){
		$this->redirect('/goodstype/type_list');
	}
	
	/**
	 * 商品类型列表
	 * 
	 */ 
	public function type_list(){		
		/*$TypeModel	=	new TypeModel();
		$typeList	=	$TypeModel->getTypeList();
		$this->assign("typeList",$typeList);*/	;
	
	    $good_type_list 	= 	get_goodstype();
	    $good_in_type 		= 	'';
	
	    $query 				= 	M()->query("SELECT a.cat_id FROM " . 'ybr_attribute' . " AS a RIGHT JOIN " . 'ybr_goods_attr' . " AS g ON g.attr_id = a.attr_id GROUP BY a.cat_id");
	    
	    foreach($query as $row){
	    	$good_in_type[$row['cat_id']]	=	1;
	    }
	    
	    $this->assign('goods_type_arr',   $good_type_list);
	    $this->assign('good_in_type', $good_in_type);		
		$this->display();
	}
	
	// 添加商品类型
	public function add_goodstype(){		
		$this->assign('act','add_goodstype_ok');
		$this->display();
	}
	
	// 处理添加商品类型
	public function add_goodstype_ok(){	
		
		$goods_type['cat_name']		=	Utils::sub_str($_POST['cat_name'], 60);
		$goods_type['attr_group']	=	Utils::sub_str($_POST['attr_group'], 255);
		$goods_type['enabled']		=	intval($_POST['enabled']);
		// 判断是否有重复
		$cat_id						=	M('GoodsType')->where(" cat_name='{$goods_type['cat_name']}'")->getField("cat_id");
		if($cat_id){
			$this->error("已存在该类型,正在为您跳转上一次页面...",U('type/add_type'));
		}
		/*入库*/
		$result		=	M('GoodsType')->add($goods_type);	
		/*isAjax*/
		if($this->isAjax()){
			if($result){
				$this->ajaxReturn(1,U('type/type_list'),1);	
			}
			else{
				$this->ajaxReturn(0,U('type/type_list'),1);
			}
		}
		else{
			if($result){
				$this->success("操作成功,正在为您跳转...",U('type/type_list'));
			}
			else{
				$this->success("操作失败,正在为您跳转...",U('type/type_list'));
			}
		}
	}
	
	// 删除商品类型
	public function del_type(){
		$id 		= 	intval($_GET['id']);		
		$name		=	M('GoodsType')->field("cat_name")->where("cat_id = '{$id}'")->getField("cat_name");
				
		$result		=	M('GoodsType')->where("cat_id='{$id}'")->delete();		
		if($result){
			// 清除该类型下的所有属性
			$attr	=	M('Attribute')->field("attr_id")->where("cat_id = '{$id}'")->select();
			
			$sql	=	" delete from ybr_attribute where attr_id "  . Utils::create_in_sql($attr);			
			M('Attribute')->query($sql);
			$sql	=	" delete from ybr_goods_attr where attr_id "  . Utils::create_in_sql($attr);
			M('GoodsAttr')->query($sql);
			// 跳转页面
			$this->error("操作成功",U('type/type_list'));
		}
		else{
			$this->error("操作失败",U('type/type_list'));
		}
	}
	
	// 修改商品类型
	public function mod_goodstype(){
				
		$goods_type = get_goodstype_info(intval($_GET['cat_id']));
		
	    if (empty($goods_type))
	    {
	        sys_msg($_LANG['cannot_found_goodstype'], 1);
	    }
	
	    $this->assign('form_act',    	'update');
	    $this->assign('goods_type',  	$goods_type);	
	    $this->assign('act',			'mod_goodstype_ok');
		$this->display('add_goodstype');
	}
	
	/**
	 * 处理修改商品类型
	 * 
	 */
	public function mod_goodstype_ok(){
		
		$CommonModel				=	new CommonModel();
		
		$goods_type['cat_name']   	= 	sub_str($_POST['cat_name'], 60);
	    $goods_type['attr_group'] 	= 	sub_str($_POST['attr_group'], 255);
	    $goods_type['enabled']    	= 	intval($_POST['enabled']);
	    $cat_id                   	= 	intval($_POST['cat_id']);
	    $old_groups               	= 	get_attr_groups($cat_id);
	    
	    if ($CommonModel->autoExecute('goods_type', $goods_type, 'UPDATE', "cat_id='$cat_id'") !== false){
	        /* 对比原来的分组 */
	        $new_groups 			= 	explode("\n", str_replace("\r", '', $goods_type['attr_group']));  // 新的分组
			
	        foreach ($old_groups AS $key=>$val){
	            $found 				= 	array_search($val, $new_groups);
	
	            if ($found === NULL || $found === false){
	                /* 老的分组没有在新的分组中找到 */
	                update_attribute_group($cat_id, $key, 0);
	            }
	            else{
	                /* 老的分组出现在新的分组中了 */
	                if ($key != $found){
	                    update_attribute_group($cat_id, $key, $found); // 但是分组的key变了,需要更新属性的分组
	                }
	            }
	        }
	        $this->ajaxReturn(1,"操作成功",1);
	    }	
	    else{
	    	$this->ajaxReturn(0,"操作失败",1);	
	    }	
	}
	
	/**
	 * 删除商品类型分类
	 * 
	 */
	public function ajax_goodstype(){

		$action	=	$_REQUEST['action'];
		
		switch($action){
			case 'del':			
				/*导入类库*/
				import("@.Plugs.Exchange");
				$exc 			= 	new Exchange("ybr_goods_type", null, 'cat_id', 'cat_name');
				$CommonModel	=	new CommonModel();
				
			    $id			 	= 	intval($_REQUEST['id']);
			
			    $name 			= 	$exc->get_name($id);
				
			    if ($exc->drop($id)){
			        admin_log(addslashes($name), 'remove', 'goods_type');
			
			        /* 清除该类型下的所有属性 */
			        $sql 		= 	"SELECT attr_id FROM " . 'ybr_attribute' . " WHERE cat_id = '$id'";
			        $arr 		= 	$CommonModel->getCol($sql);
					//t($sql);
			        M()->execute("DELETE FROM " . 'ybr_attribute' . " WHERE attr_id " . db_create_in($arr));
			        M()->execute("DELETE FROM " . 'ybr_goods_attr' . " WHERE attr_id " . db_create_in($arr));
					$this->ajaxReturn(1,"操作成功",1);
			    }
			    else{
			        $this->ajaxReturn(0,"操作失败",1);
			    }
				break;
			case 'edit':				
				$field	=	$_REQUEST['field'];
				$value	=	$_REQUEST['value'];
				$id		=	$_REQUEST['id'];
				/*判断商品类型名城是否重复*/
				$is_exsists	=	M('GoodsType')->where(" cat_id != '{$id}' and  cat_name='{$value}' ")->find();
				if($is_exsists){
					$this->ajaxReturn(0,"该商品分类已经存在",1);
				}				
				$result		=	M('GoodsType')->where("cat_id = '{$id}'")->setField($field,$value);
				
				if($result){
					$this->ajaxReturn(1,$value,1);
				}
				else{
					$this->ajaxReturn(0,"",1);
				}
				break;
			case 'add':/*添加商品类型*/
				break;
			default:
				break;
		}
		
	}
	
	// 商品回收站
	public function goods_trash(){
		
	}	
	
	
	
		
}









